package service

import (
	"context"
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
	"weibo_1006/global"
	"weibo_1006/internal/model"
	"weibo_1006/internal/params"
)

func AddWeibo(c *gin.Context) {
	var req params.AddWeiboForm

	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "验证失败",
			"data":    "",
		})
		return
	}

	var weibo model.Weibo

	weibo = model.Weibo{
		UserId:       req.UserId,
		Title:        req.Title,
		ContentImg:   req.ContentImg,
		ConfigText:   req.ConfigText,
		CreateStatus: req.CreateStatus,
	}
	err := weibo.AddWeibo()
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "发布失败",
			"data":    err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "发布成功",
		"data":    weibo.Id,
	})

}

func DeleteWeibo(c *gin.Context) {
	var req params.DeleteWeiboForm

	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "验证失败",
			"data":    "",
		})
		return
	}

	var weibo model.Weibo

	weibo.Id = req.Id
	err := weibo.DeleteWeibo()
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "删除失败",
			"data":    err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "删除成功",
		"data":    "",
	})
}

func FindWeibo(c *gin.Context) {
	var req params.FindWeiboForm

	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "验证失败",
			"data":    "",
		})
		return
	}

	var weibo model.Weibo

	weibo.Id = req.Id
	err := weibo.FindWeibo()
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "查看失败",
			"data":    err.Error(),
		})
		return
	}

	global.RDB.Set(context.Background(), "listWeibo", weibo, time.Minute*240)

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "查看成功",
		"data":    weibo,
	})
}

func CommentWeibo(c *gin.Context) {
	var req params.CommentWeiboFrom

	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "验证失败",
			"data":    "",
		})
		return
	}

	var comment model.Comment

	comment = model.Comment{
		UserId:  req.UserId,
		WeiboId: req.WeiboId,
		Content: req.Content,
	}
	err := comment.AddComment()
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "评论失败",
			"data":    err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "评论成功",
		"data":    comment.Content,
	})
}

func CommentListWeibo(c *gin.Context) {
	var req params.CommentListWeiboFrom

	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "验证失败",
			"data":    "",
		})
		return
	}

	var comment model.Comment

	listComment, err := comment.ListComment(req.WeiboId)

	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "加载失败",
			"data":    err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "加载成功",
		"data":    listComment,
	})
}

func CommentDeleteWeibo(c *gin.Context) {
	var req params.CommentDeleteWeiboFrom

	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "验证失败",
			"data":    "",
		})
		return
	}

	var comment model.Comment
	comment.Id = req.Id
	err := comment.DeleteComment()
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "删除失败",
			"data":    err.Error(),
		})
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "删除成功",
		"data":    "",
	})

}
